/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "Input.h"

#include "intern/DxInput.h"




uint8 S_InputKey::e_ESC = DIK_ESCAPE;


uint8 S_InputKey::e_KEY_1 = DIK_1;
uint8 S_InputKey::e_KEY_2 = DIK_2;
uint8 S_InputKey::e_KEY_3 = DIK_3;
uint8 S_InputKey::e_KEY_4 = DIK_4;
uint8 S_InputKey::e_KEY_5 = DIK_5;
uint8 S_InputKey::e_KEY_6 = DIK_6;
uint8 S_InputKey::e_KEY_7 = DIK_7;
uint8 S_InputKey::e_KEY_8 = DIK_8;
uint8 S_InputKey::e_KEY_9 = DIK_9;
uint8 S_InputKey::e_KEY_0 = DIK_0;

uint8 S_InputKey::e_KEY_减 = DIK_MINUS;
uint8 S_InputKey::e_KEY_加 = DIK_EQUALS;
uint8 S_InputKey::e_KEY_退格 = DIK_BACK;

uint8 S_InputKey::e_KEY_TAB = DIK_TAB;

uint8 S_InputKey::e_KEY_A = DIK_A;
uint8 S_InputKey::e_KEY_B = DIK_B;
uint8 S_InputKey::e_KEY_C = DIK_C;
uint8 S_InputKey::e_KEY_D = DIK_D;
uint8 S_InputKey::e_KEY_E = DIK_E;
uint8 S_InputKey::e_KEY_F = DIK_F;
uint8 S_InputKey::e_KEY_G = DIK_G;
uint8 S_InputKey::e_KEY_H = DIK_H;
uint8 S_InputKey::e_KEY_I = DIK_I;
uint8 S_InputKey::e_KEY_J = DIK_J;
uint8 S_InputKey::e_KEY_K = DIK_K;
uint8 S_InputKey::e_KEY_L = DIK_L;
uint8 S_InputKey::e_KEY_M = DIK_M;
uint8 S_InputKey::e_KEY_N = DIK_N;
uint8 S_InputKey::e_KEY_O = DIK_O;
uint8 S_InputKey::e_KEY_P = DIK_P;
uint8 S_InputKey::e_KEY_Q = DIK_Q;
uint8 S_InputKey::e_KEY_R = DIK_R;
uint8 S_InputKey::e_KEY_S = DIK_S;
uint8 S_InputKey::e_KEY_T = DIK_T;
uint8 S_InputKey::e_KEY_U = DIK_U;
uint8 S_InputKey::e_KEY_V = DIK_V;
uint8 S_InputKey::e_KEY_W = DIK_W;
uint8 S_InputKey::e_KEY_X = DIK_X;
uint8 S_InputKey::e_KEY_Y = DIK_Y;
uint8 S_InputKey::e_KEY_Z = DIK_Z;

uint8 S_InputKey::e_KEY_方括号左 = DIK_LBRACKET;
uint8 S_InputKey::e_KEY_方括号右 = DIK_RBRACKET;


uint8 S_InputKey::e_KEY_回车 = DIK_RETURN;
uint8 S_InputKey::e_KEY_点 = DIK_PERIOD;

uint8 S_InputKey::e_KEY_分号 = DIK_SEMICOLON;
uint8 S_InputKey::e_KEY_引号 = DIK_APOSTROPHE;
uint8 S_InputKey::e_KEY_反斜杠 = DIK_BACKSLASH;


uint8 S_InputKey::e_KEY_空格 = DIK_SPACE;


uint8 S_InputKey::e_KEY_F1 = DIK_F1;
uint8 S_InputKey::e_KEY_F2 = DIK_F2;
uint8 S_InputKey::e_KEY_F3 = DIK_F3;
uint8 S_InputKey::e_KEY_F4 = DIK_F4;
uint8 S_InputKey::e_KEY_F5 = DIK_F5;
uint8 S_InputKey::e_KEY_F6 = DIK_F6;
uint8 S_InputKey::e_KEY_F7 = DIK_F7;
uint8 S_InputKey::e_KEY_F8 = DIK_F8;
uint8 S_InputKey::e_KEY_F9 = DIK_F9;
uint8 S_InputKey::e_KEY_F10 = DIK_F10;
uint8 S_InputKey::e_KEY_F11 = DIK_F11;
uint8 S_InputKey::e_KEY_F12 = DIK_F12;



uint8 S_InputKey::e_KEY_BACKQUOTE;
uint8 S_InputKey::e_KEY_逗号 = DIK_COMMA;
uint8 S_InputKey::e_KEY_句号 = DIK_PERIOD;
uint8 S_InputKey::e_KEY_斜杠 = DIK_SLASH;

uint8 S_InputKey::e_KEY_方向_右 = DIK_RIGHT;
uint8 S_InputKey::e_KEY_方向_左 = DIK_LEFT;
uint8 S_InputKey::e_KEY_方向_下 = DIK_DOWN;
uint8 S_InputKey::e_KEY_方向_上 = DIK_UP;
	
uint8 S_InputKey::e_KEY_小键盘数字键_除 = DIK_DIVIDE;
uint8 S_InputKey::e_KEY_小键盘数字键_乘 = DIK_MULTIPLY;
uint8 S_InputKey::e_KEY_小键盘数字键_减 = DIK_MEDIASTOP;
uint8 S_InputKey::e_KEY_小键盘数字键_加 = DIK_ADD;
uint8 S_InputKey::e_KEY_小键盘数字键_回车 = DIK_NUMPADENTER;
	
uint8 S_InputKey::e_KEY_小键盘数字键_1 = DIK_NUMPAD1;
uint8 S_InputKey::e_KEY_小键盘数字键_2 = DIK_NUMPAD2;
uint8 S_InputKey::e_KEY_小键盘数字键_3 = DIK_NUMPAD3;
uint8 S_InputKey::e_KEY_小键盘数字键_4 = DIK_NUMPAD4;
uint8 S_InputKey::e_KEY_小键盘数字键_5 = DIK_NUMPAD5;
uint8 S_InputKey::e_KEY_小键盘数字键_6 = DIK_NUMPAD6;
uint8 S_InputKey::e_KEY_小键盘数字键_7 = DIK_NUMPAD7;
uint8 S_InputKey::e_KEY_小键盘数字键_8 = DIK_NUMPAD8;
uint8 S_InputKey::e_KEY_小键盘数字键_9 = DIK_NUMPAD9;
uint8 S_InputKey::e_KEY_小键盘数字键_0 = DIK_NUMPAD0;
uint8 S_InputKey::e_KEY_小键盘数字键_点 = DIK_DECIMAL;
	 

uint8 S_InputKey::e_KEY_大写锁定 = DIK_CAPITAL;
uint8 S_InputKey::e_KEY_左SHIFT = DIK_LSHIFT;
uint8 S_InputKey::e_KEY_左CTRL = DIK_LCONTROL;
uint8 S_InputKey::e_KEY_左ALT = DIK_LMENU;
uint8 S_InputKey::e_KEY_右SHIFT = DIK_RSHIFT;
uint8 S_InputKey::e_KEY_右CTRL = DIK_RCONTROL;
uint8 S_InputKey::e_KEY_右ALT = DIK_RMENU;
//DIK_RWIN

uint8 S_InputKey::e_KEY_上一页 = DIK_PRIOR;
uint8 S_InputKey::e_KEY_下一页 = DIK_NEXT;

uint8 S_InputKey::e_KEY_删除 = DIK_DELETE;


uint8 S_InputKey::e_KEY_左系统徽标 = DIK_LWIN;
uint8 S_InputKey::e_KEY_右系统徽标 = DIK_RWIN;
uint8 S_InputKey::e_KEY_菜单 = DIK_APPS;



//int8* S_输入环境::m_Keys = (int8*)calloc(257, sizeof(int8));
//static CDInputKeyboard g_DXInput;
S_输入环境* f_input_create输入设备() {
	S_输入环境* inputCtx = (S_输入环境*)malloc(sizeof(S_输入环境));
	inputCtx->m_Key.Keys = (uint8*)(calloc(256, sizeof(uint8)));
	inputCtx->m_Key.Keys状态 = (uint8*)(calloc(256, sizeof(int8)));
	inputCtx->m_鼠标全局坐标 = {};

	inputCtx->m_设备是否激活 = false;
	inputCtx->m_InputDev = new CDInputKeyboard;

	inputCtx->m_鼠标设备 = nullptr;
	inputCtx->m_键盘设备 = nullptr;
	inputCtx->m_鼠标 = (S_结构对象指针)malloc(sizeof(S_鼠标属性));

	return inputCtx;
}

void f_input_release输入设备(S_输入环境* ctx) {
	((CDInputKeyboard*)ctx->m_InputDev)->f_释放设备(&(ctx->m_鼠标设备));
	((CDInputKeyboard*)ctx->m_InputDev)->f_释放设备(&(ctx->m_键盘设备));

	free(ctx->m_Key.Keys);
	free(ctx->m_Key.Keys状态);
	delete ctx->m_InputDev;
	free(ctx);
}

void f_initInput(S_输入环境* ctx, S_结构对象指针 窗口勾丙, S_结构对象指针 窗口实例) {
	//((CDInputKeyboard*)ctx->m_InputDev)->InitInput((HWND)窗口勾丙, *((HINSTANCE*)窗口实例));
	((CDInputKeyboard*)ctx->m_InputDev)->InitInput((HWND)窗口勾丙, (HINSTANCE)窗口实例);

	uint8 num = ((CDInputKeyboard*)ctx->m_InputDev)->EnumKeyboard();

	((CDInputKeyboard*)ctx->m_InputDev)->f_获取设备(&(ctx->m_鼠标设备), E_输入设备类型预设::e_In鼠标);
	((CDInputKeyboard*)ctx->m_InputDev)->f_获取设备(&(ctx->m_键盘设备), E_输入设备类型预设::e_In键盘);
}

/*uint8 f_枚举输入设备(S_输入环境* ctx) {
	uint8 num = ((CDInputKeyboard*)ctx->m_InputDev)->EnumKeyboard();
	return num;
}

bool f_枚举输入设备(S_输入环境* ctx, E_输入设备类型预设 设备类型预设) {
	switch (设备类型预设)
	{
	case E_输入设备类型预设::e_In鼠标:
		ctx->m_鼠标 = (S_结构对象指针)malloc(sizeof(S_鼠标属性));
		break;
	default:
		break;
	}

	//S_键盘;
	inputCtx->m_InputDev->f_获取设备(&(ctx->m_鼠标设备), 设备类型预设);
	return inputCtx->m_InputDev->f_获取设备(&(ctx->m_键盘设备), E_输入设备类型预设::e_In键盘);
}*/

//bool f_释放输入设备(S_输入环境* ctx) {
//	return true;
//}

void f_取输入状态(S_输入环境* ctx) {
	//inputCtx->m_InputDev->f_获取设备(&(ctx->m_鼠标设备), E_输入设备类型预设::e_In鼠标);
	//inputCtx->m_InputDev->f_获取设备(&(ctx->m_键盘设备), E_输入设备类型预设::e_In键盘);

	((CDInputKeyboard*)ctx->m_InputDev)->f_get鼠标状态(&ctx->m_鼠标设备, ctx->m_鼠标);
	((CDInputKeyboard*)ctx->m_InputDev)->f_get键盘状态(ctx);
}














uint8 f_getKeyDown(const S_键盘& ctx) {
	/*for (uint8 i = 1; i < 128; ++i) {
		if(ctx.Keys[i]) std::cout << int32(ctx.Keys[i]) << " " << int(i) << std::endl;
	}*/
	for (uint16 i = 0; i < 256; ++i) {
		if(ctx.Keys[i]) return i;
	}
	return 0;
}

uint8 f_getKeyUn(const S_键盘& ctx) {
	for (uint16 i = 0; i < 256; ++i) {
		if (ctx.Keys状态[i] == DEF_按键状态_放开) return i;
	}
	return 0;
}

uint16 f_getDX按键AsciiKeyDown(uint8 key) {
	static HKL layout = GetKeyboardLayout(0);
	static uint8 State[256];
	uint16 ksy = 0;

	if (GetKeyboardState(State) == FALSE)
		return 0;
	UINT vk = MapVirtualKeyEx(key, 1, layout);
	ToAsciiEx(vk, key, State, &ksy, 0, layout);

	if (0 == ksy) {
		switch (key)
		{
		case DIK_NUMPAD1: return 49;
		case DIK_NUMPAD2: return 50;
		case DIK_NUMPAD3: return 51;
		case DIK_NUMPAD4: return 52;
		case DIK_NUMPAD5: return 53;
		case DIK_NUMPAD6: return 54;
		case DIK_NUMPAD7: return 55;
		case DIK_NUMPAD8: return 56;
		case DIK_NUMPAD9: return 57;
		case DIK_NUMPAD0: return 48;

		case DIK_DECIMAL: return 46;
		case DIK_NUMPADENTER: return '\n';
		default:
			break;
		}
	}

	return ksy;

	//if(ctx.Keys[i]) {
		

		/*switch (i) {
			case DIK_1: return '1';
			case DIK_2: return '2';
			case DIK_3: return '3';
			case DIK_4: return '4';
			case DIK_5: return '5';
			case DIK_6: return '6';
			case DIK_7: return '7';
			case DIK_8: return '8';
			case DIK_9: return '9';
			case DIK_0: return '0';

			case DIK_A: return 'A';
			case DIK_B: return 'B';
			case DIK_C: return 'C';
			case DIK_D: return 'D';
			case DIK_E: return 'E';
			case DIK_F: return 'F';
			case DIK_G: return 'G';
			case DIK_H: return 'H';
			case DIK_I: return 'I';
			case DIK_J: return 'J';
			case DIK_K: return 'K';
			case DIK_L: return 'L';
			case DIK_M: return 'M';
			case DIK_N: return 'N';
			case DIK_O: return 'O';
			case DIK_P: return 'P';
			case DIK_Q: return 'Q';
			case DIK_R: return 'R';
			case DIK_S: return 'S';
			case DIK_T: return 'T';
			case DIK_U: return 'U';
			case DIK_V: return 'V';
			case DIK_W: return 'W';
			case DIK_X: return 'X';
			case DIK_Y: return 'Y';
			case DIK_Z: return 'Z';

			case DIK_SPACE: return ' ';
			case DIK_GRAVE: return '`';
			default:
				break;
		}*/

		//std::cout << int32(ctx.Keys[S_Input::e_KEY_A]) << " " << i << std::endl;
	//}
	
	return 0;
	
}

int16 f_getDX按键小键盘值(int8 key) {
	switch (key)
	{
	case DIK_NUMPAD1: return 1;
	case DIK_NUMPAD2: return 2;
	case DIK_NUMPAD3: return 3;
	case DIK_NUMPAD4: return 4;
	case DIK_NUMPAD5: return 5;
	case DIK_NUMPAD6: return 6;
	case DIK_NUMPAD7: return 7;
	case DIK_NUMPAD8: return 8;
	case DIK_NUMPAD9: return 9;
	case DIK_NUMPAD0: return 0;
	default:
		break;
	}
	return -1;
}














